package com.jiedeng.controller;

import com.jiedeng.dao.ICorpRepository;
import com.jiedeng.dao.IFilePathRepository;
import com.jiedeng.dao.ISysModuleRepository;
import com.jiedeng.dao.ISysNamedRepository;
import com.jiedeng.model.Corp;
import com.jiedeng.model.FilePath;
import com.jiedeng.model.SysModule;
import com.jiedeng.model.SysNamed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 覃远龙
 * @ClassName: OtherSettingController
 * @Description:
 * @date 2017/5/12 10:19
 */
@RestController
@RequestMapping("/other")
public class OtherSettingController {

    @Autowired
    IFilePathRepository filePathRepository;

    @Autowired
    ISysNamedRepository sysNamedRepository;

    @Autowired
    ICorpRepository corpRepository;

    @Autowired
    ISysModuleRepository sysModuleRepository;

    //---------------- t_FilePath table  -----------------//
    @RequestMapping(value = "/filepath/all", method = RequestMethod.GET)
    public List<FilePath> findAllFilePath(){
        List<FilePath> filePaths =  filePathRepository.findAll();
        for (FilePath filePath : filePaths) {
            if(filePath.getCorpID() > 0){
                Corp corp = corpRepository.getOne(filePath.getCorpID());
                if(corp != null){
                    filePath.setCorpName(corp.getCaption());
                }
            }
            if(filePath.getCorpID() == -1){
                filePath.setCorpName("全局");
            }
        }

        return filePaths;
    }

    @RequestMapping(value = "/filepath/save", method = RequestMethod.POST)
    public FilePath saveFilePath(@RequestBody FilePath filePath){
        return filePathRepository.save(filePath);
    }

    @RequestMapping(value = "/filepath/delete", method = RequestMethod.POST)
    public void deleteFilePath(long id){
        filePathRepository.deleteById(id);
    }

    //---------------- sysmodule table  -----------------//
    @RequestMapping(value = "/module/all", method = RequestMethod.GET)
    public List<SysModule> findAllSysModule(){
        List<SysModule> modules = sysModuleRepository.findAll();

        return modules;
    }

    @RequestMapping(value = "/module/save", method = RequestMethod.POST)
    public SysModule saveModule(@RequestBody SysModule named){
        return sysModuleRepository.save(named);
    }

    @RequestMapping(value = "/module/delete", method = RequestMethod.POST)
    public void deleteModule(int id){
        sysModuleRepository.deleteById(id);
    }

    //---------------- SysNamed table  -----------------//
    @RequestMapping(value = "/named/all", method = RequestMethod.GET)
    public List<SysNamed> findAllNamed(){
        return sysNamedRepository.findAll();
    }

    @RequestMapping(value = "/named/save", method = RequestMethod.POST)
    public SysNamed saveNamed(@RequestBody SysNamed named){
        return sysNamedRepository.save(named);
    }

    @RequestMapping(value = "/nameddelete", method = RequestMethod.POST)
    public void deleteNamed(int id){
        sysNamedRepository.deleteById(id);
    }

}
